package com.example.exam.service.impl;

import com.example.exam.dao.*;
import com.example.exam.entity.*;
import com.example.exam.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * (Student)表服务实现类
 *
 * @author makejava
 * @since 2021-09-04 15:02:45
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao studentDao;
    @Resource
    private PaperDao paperDao;

    @Resource
    private QuestionDao questionDao;

    @Resource
    private ChoiceDao choiceDao;

    @Resource
    private FillbacksDao fillbacksDao;

    @Resource
    private TorfDao torfDao;

    @Resource
    private SubjectiveDao subjectiveDao;

    @Resource
    private StuansDao stuansDao;

    @Resource
    private GradeDao gradeDao;

    @Resource
    private MessageDao messageDao;

    /**
     * 通过ID查询单条数据
     *
     * @param stuid 主键
     * @return 实例对象
     */
    @Override
    public Student queryById(Integer stuid) {
        return this.studentDao.queryById(stuid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Student> queryAllByLimit(int offset, int limit) {
        return this.studentDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Student insert(Student student) {
        this.studentDao.insert(student);
        return student;
    }

    /**
     * 修改数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Student update(Student student) {
        this.studentDao.update(student);
        return this.queryById(student.getStuid());
    }

    /**
     * 通过主键删除数据
     *
     * @param stuid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer stuid) {
        return this.studentDao.deleteById(stuid) > 0;
    }

    @Override
    public List<Student> queryAll() {
        return this.studentDao.queryAll2();
    }

    @Override
    public List<Student> queryByAssId(Integer assId) {
        return this.studentDao.queryByAssId(assId);
    }

    public List<Question> selectbystuid(Integer stuid) {//HttpSession session){  //通过userid获得可选择的试题
        //int stuid= (int) session.getAttribute("userId");

        Student student = studentDao.queryById(1004);
//        System.out.println(student.toString());
        int majorid = student.getMajorid();
        List<Question> question_list = questionDao.queryBymajor(majorid);
        return question_list;
    }

    public List<Choice> selectchoice(Integer questionId) {

        List<Choice> list_choice = new ArrayList<>();
        Question question = questionDao.queryById(questionId);
        int paperid = question.getPaperid();
        Paper paper = paperDao.queryById(paperid);
        String s = paper.getPaperquestion();
        String[] str = s.split(",");
        int nums[] = new int[str.length];
        for (int i = 0; i < str.length; i++) {
            nums[i] = Integer.parseInt(str[i]);
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] >= 1 && nums[i] <= 1000) {
                Choice choice = choiceDao.queryById(nums[i]);
                list_choice.add(choice);
            }
        }
        return list_choice;
    }

    public List<Torf> selecttorf(Integer questionId) {


        List<Torf> list_torf = new ArrayList<>();
        Question question = questionDao.queryById(questionId);
        int paperid = question.getPaperid();
        Paper paper = paperDao.queryById(paperid);
        String s = paper.getPaperquestion();
        String[] str = s.split(",");
        int nums[] = new int[str.length];
        for (int i = 0; i < str.length; i++) {
            nums[i] = Integer.parseInt(str[i]);
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 2000 && nums[i] <= 3000) {
                Torf torf = torfDao.queryById(nums[i]);
                list_torf.add(torf);
            }
        }
        return list_torf;
    }

    public List<Subjective> selectsubjective(Integer questionId) {
        int j = 0;
        List<Subjective> list_subjective = new ArrayList<>();
        Question question = questionDao.queryById(questionId);
        int paperid = question.getPaperid();
        Paper paper = paperDao.queryById(paperid);
        String s = paper.getPaperquestion();
        String[] str = s.split(",");
        int nums[] = new int[str.length];
        for (int i = 0; i < str.length; i++) {
            nums[i] = Integer.parseInt(str[i]);
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 3000 && nums[i] <= 4000) {
                Subjective subjective = subjectiveDao.queryById(nums[i]);
                list_subjective.add(subjective);
            }
        }
        return list_subjective;
    }

    public List<Fillbacks> selectfillbacks(Integer questionId) {
        int j = 0;
        List<Fillbacks> list_fillbacks = new ArrayList<>();
        Question question = questionDao.queryById(questionId);
        int paperid = question.getPaperid();
        Paper paper = paperDao.queryById(paperid);
        String s = paper.getPaperquestion();
        String[] str = s.split(",");
        int nums[] = new int[str.length];
        for (int i = 0; i < str.length; i++) {
            nums[i] = Integer.parseInt(str[i]);
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 1000 && nums[i] <= 2000) {
                Fillbacks fillbacks = fillbacksDao.queryById(nums[i]);
                list_fillbacks.add(fillbacks);
            }
        }
        return list_fillbacks;
    }

    @Override
    public Stuans insert_answer(String s, Integer questionId, HttpSession session) {
        int stuId = (int) session.getAttribute("userId");
        StringBuffer choice = new StringBuffer();
        StringBuffer fillbacks = new StringBuffer();
        StringBuffer torf = new StringBuffer();
        StringBuffer subjective = new StringBuffer();
        String choiceAns = new String();
        String fillAns = new String();
        String torfAns = new String();
        String subAns = new String();
        String str[] = s.split("#");
        for (int i = 0; i < 4; i++) {
            choice.append(str[i] + ",");
        }
        choice.append(str[4]);
        choiceAns = String.valueOf(choice);

        for (int i = 5; i < 9; i++) {
            fillbacks.append(str[i] + ",");
        }
        fillbacks.append(str[9]);
        fillAns = String.valueOf(fillbacks);

        for (int i = 10; i < 14; i++) {
            torf.append(str[i] + ",");
        }
        torf.append(str[14]);
        torfAns = String.valueOf(torf);

        for (int i = 15; i < 16; i++) {
            subjective.append(str[i] + ",");
        }
        subjective.append(str[16]);
        subAns = String.valueOf(subjective);
        Stuans stuans = new Stuans();
        stuans.setStuid(stuId);
        stuans.setChoiceans(choiceAns);
        stuans.setFillans(fillAns);
        stuans.setQuestionid(questionId);
        stuans.setTorfans(torfAns);
        stuans.setSubans(subAns);
        stuansDao.insert(stuans);
        return stuans;
    }

    public List<Grade> select_grade(Integer stuid) {//HttpSession session){
        //int stuid = (int) session.getAttribute("userId");
        return gradeDao.selectbyid(stuid);
    }

    public List<Message> selectmessage() {
        return messageDao.selectall();
    }

}